package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.explore.demo.common.ResultCode;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.pojo.Company;
import com.explore.demo.pojo.Department;
import com.explore.demo.mapper.DepartmentMapper;
import com.explore.demo.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-04-18
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService
{

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public ServerResponse addDepartment(Department department)
    {
        if (department == null){
            return ServerResponse.createBySuccessMessage(ResultCode.DEPARTMENT_IS_NULL.getDescription(),
                                                         ResultCode.DEPARTMENT_IS_NULL.getCode(),
                                                         null);
        }
        return departmentMapper.insert(department) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.DEPARTMENT_INSERT_FAIL.getDescription(),
                                                     ResultCode.DEPARTMENT_INSERT_FAIL.getCode(),
                                                     null);
    }

    @Override
    public ServerResponse deleteByDepartmentName(String departmentName){
        if (departmentName == null){
            return ServerResponse.createBySuccessMessage(ResultCode.DEPARTMENT_NAME_IS_NULL.getDescription(),
                                                         ResultCode.DEPARTMENT_NAME_IS_NULL.getCode(),
                                                         null);
        }
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("department_name", departmentName);
        Department department = departmentMapper.selectOne(wrapper);
        UpdateWrapper<Department> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("deleted",1);
        return departmentMapper.update(department,updateWrapper) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.DEPARTMENT_DELETE_FAIL.getDescription(),
                                                     ResultCode.DEPARTMENT_DELETE_FAIL.getCode(),
                                                     null);
    }

    @Override
    public ServerResponse queryAll()
    {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper
                .ge("id",1);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     departmentMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse queryByDepartmentName(String departmentName)
    {
        if (departmentName == null){
            return ServerResponse.createBySuccessMessage(ResultCode.DEPARTMENT_NAME_IS_NULL.getDescription(),
                                                         ResultCode.DEPARTMENT_NAME_IS_NULL.getCode(),
                                                         null);
        }
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper
                .eq("department_name",departmentName);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     departmentMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse updateDepartment(Department department)
    {
        if (department == null){
            return ServerResponse.createBySuccessMessage(ResultCode.DEPARTMENT_IS_NULL.getDescription(),
                                                         ResultCode.DEPARTMENT_IS_NULL.getCode(),
                                                         null);
        }
        return departmentMapper.updateById(department) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.UPDATE_FAIL.getDescription(),
                                                     ResultCode.UPDATE_FAIL.getCode(),
                                                     null);
    }
}
